package top.littlejiang.onlinexam.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.utils.JWTUtils;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamUser;
import top.littlejiang.onlinexam.service.OlexamUserService;
import top.littlejiang.onlinexam.vo.ModifyInformationVO;

import java.util.List;


/**
 * @author chen
 * @Description
 * @since 2021/2/27 18:54
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    OlexamUserService userService;

    /**
     * 修改个人信息（密码）
     * @param vo
     * @return
     */
    @Log("修改个人信息")
    @PostMapping("/modify_information")
    @ResponseBody
    @RequiresPermissions("olexam:basis:person")
    public Result modifyInformation(@Validated @RequestBody ModifyInformationVO vo){
        userService.modifyOne(vo);
        return Result.ok();
    }

    /**
     * 获取个人信息
     * @return
     */
    @PostMapping("/getUserInfo")
    @ResponseBody
    @RequiresPermissions("olexam:basis:person")
    public Result getUserInfo(){
        OlexamUser userById = userService.getUserById(JWTUtils.getUserId());
        return Result.ok(userById);
    }

    /**
     * 前端select 远程查询接口
     * @param user
     * @return
     */
    @PostMapping("/getuserselect")
    @ResponseBody
    public Result getUserSelect(@RequestBody JSONObject user){
        List<OlexamUser> list = userService.getUserSelect(user.getString("userName"));
        return Result.ok(list);
    }

}
